package javaMSCRM;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
/**
 * @author Zoran IVANOV
 *
 */
public class LookupField extends JPanel implements ActionListener{
	JavaMSCRM JavaMSCRM = null;
	String RecordName ="";
	String RecordId = "";
	String EntityName = "";
	LookupLabel JTF;
	JButton LookupButton = new JButton();
	
	private static final long serialVersionUID = 1L;

	/**
	 * @author Zoran IVANOV
	 *
	 */
	public class LookupLabel extends JLabel{
		private static final long serialVersionUID = 1L;
		public LookupLabel(String EN){
			setFont(new Font("Arial", Font.PLAIN, 12));
			setToolTipText("Double click for details");
			final String EN1 = EN;
			addMouseListener(new MouseAdapter(){
		        public void mouseClicked(MouseEvent e){
		        	if (e.getClickCount() == 2){
		        		if(EN1.equals(com.microsoft.schemas.crm._2007.webservices.EntityName.CONTACT.toString())){
		        			new JavaMSCRMContact(JavaMSCRM, RecordId);
		        		}else if(EN1.equals(com.microsoft.schemas.crm._2007.webservices.EntityName.TRANSACTIONCURRENCY.toString())){
		        			//not implemented
		        		}else if(EN1.equals(com.microsoft.schemas.crm._2007.webservices.EntityName.ACCOUNT.toString())){
		        			new JavaMSCRMAccount(JavaMSCRM, RecordId);
		        		}
		            }
		         }
		     } );
		}
	}
	
	/**
	 * @param JavaMSCRM
	 * @param EntityName
	 */
	public LookupField(JavaMSCRM JavaMSCRM, String EntityName){
		this.JavaMSCRM = JavaMSCRM;
		this.EntityName = EntityName;
		RecordId = "";
		RecordName = "";		
		JTF = new LookupLabel(this.EntityName);
		this.JTF.setText("");	
		Border whiteline = BorderFactory.createMatteBorder(0, 2, 0, 0, Color.white);
		this.JTF.setBorder(whiteline);
		this.JTF.setBackground(Color.WHITE);		
		setBackground(Color.WHITE);
		setLayout(new BorderLayout(0,0));
		Border blackline = BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black);
		setBorder(blackline);
		Border leftblackline = BorderFactory.createMatteBorder(0, 1, 0, 0, Color.black);
		LookupButton = new JButton(new ImageIcon("C:/Users/Zoran/workspace/JavaMSCRM/src/lookup.gif"));
		LookupButton.setBorder(leftblackline);
		LookupButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
		LookupButton.addActionListener(this);
		add(LookupButton, BorderLayout.LINE_END);
		add(this.JTF, BorderLayout.LINE_START);	
	}
	
	/**
	 * @param RecordId
	 * @param RecordName
	 */
	public void setValue(String RecordId, String RecordName){
		this.RecordId = RecordId;
		this.JTF.setText(RecordName);
		this.RecordName = RecordName;
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource () == LookupButton){
			new LookupRecords(JavaMSCRM, this);
        }
		
	}
}
